package org.zhangchao.poi.excel.create;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.zhangchao.poi.excel.common.template.ExcelTemplateParam;

/**
 * Excle 创建接口
 * @author zhangchao
 *
 */
public abstract class ExcelCreate implements ExcelCreatable {
	
	/** SXSSFWorkbook */
	protected SXSSFWorkbook workbook;
	
	/** sheet */
	protected Sheet sheet;
	
	/** sheetName */
	private String sheetName;
	
	public ExcelCreate() {
		workbook = new SXSSFWorkbook(100);
	}
	
	/**
	 * -1, > 0
	 * @param i -1, > 0
	 */
	public ExcelCreate(int i) {
		workbook = new SXSSFWorkbook(100);
	}
	
	//@Override
	public Sheet createSheet() {
		sheet = workbook.createSheet();
		return sheet;
	}
	
	//@Override
	public Sheet createSheet(String sheetname) {
		sheet = workbook.createSheet(sheetname);
		return sheet;
	}
	
	//@Override
	public Row createRow(int i) {
		if(null == sheet) {
			if(null == sheetName || "".equals(sheetName)) {
				createSheet();
			} else {
				createSheet(sheetName);
			}
		}
		Row row = sheet.createRow(i);
		createRow(row);
		return row;
	}
	
	//@Override
	public Row copyRow(int i, Row row) {
		if (null == sheet) {
			if(null == sheetName || "".equals(sheetName)) {
				createSheet();
			} else {
				createSheet(sheetName);
			}
		}
		Row newRow = sheet.createRow(i);
		//设置行高
		newRow.setHeightInPoints(row.getHeightInPoints());
		return newRow;
	}

	//@Override
	public abstract void createRow(Row row);
	
	//@Override
	public abstract void create();
	
	//@Override
	public abstract ExcelTemplateParam getExcelTemplateaParam();
	
	//@Override
	public void create(String sheetName) {
		this.sheetName = sheetName;
		create();
	}

	//@Override
	public Cell createCell(Row row, int i) {
		Cell cell = row.createCell(i);
		return cell;
	}
	
	//@Override
	public Cell copyCell(Row row, int i, Cell cell) {
		Cell newCell = row.createCell(i);
		//设置样式
		CellStyle cellStyle = workbook.createCellStyle();
		cellStyle.cloneStyleFrom(cell.getCellStyle());
		newCell.setCellStyle(cellStyle);
		//设置值
		switch (cell.getCellType()) {
		case Cell.CELL_TYPE_BLANK:
			newCell.setCellValue("");
			break;
		case Cell.CELL_TYPE_BOOLEAN:
			newCell.setCellValue(cell.getBooleanCellValue());
			break;
		case Cell.CELL_TYPE_FORMULA:
			newCell.setCellValue(cell.getCellFormula());
			break;
		case Cell.CELL_TYPE_NUMERIC:
			newCell.setCellValue(cell.getNumericCellValue());
			break;
		case Cell.CELL_TYPE_STRING:
			newCell.setCellValue(cell.getStringCellValue());
			break;
		default:
			break;
		}
		return newCell;
	}
	
	//@Override
	public void handelMergedRegion(List<CellRangeAddress> craList) {
		for (CellRangeAddress cra :craList) {
			sheet.addMergedRegion(cra);
		}
	}
	
	public void flushRows(int i) throws IOException {
		((SXSSFSheet)sheet).flushRows(i);
	}
	
	//@Override
	public void write(OutputStream stream) throws IOException {
		workbook.write(stream);
		stream.close();
		workbook.dispose();
	}

	public String getSheetName() {
		return sheetName;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}
	
}
